﻿
namespace Patterns4Net.Tests
{
    using System;
    using System.Dynamic;
    using System.Linq;
    using NUnit.Framework;
    using System.Diagnostics.Contracts;
    using Patterns4Net.Core.CecilExtensions;

    /// <summary>
    /// Simple integration test that checks whether code contracts are 
    /// correctly set up.
    /// </summary>
    [TestFixture]
    public class CodeContractsTest
    {
#if CONTRACTS_FULL
        private bool failed;

        [SetUp]
        public void RegisterContractFailHandler()
        {
            Contract.ContractFailed += ContractFailed;
        }

        /// <summary>
        /// Tests whether contracts work on method <see cref="ReflectionToCecil.GetModuleDefinition"/>.
        /// We have registered custom handler for contract failture in set up.
        /// We will cause failture trough icorrect method call
        /// and then we will check whether our handler was called.
        /// </summary>
        [Test]
        public void ReflectionToCecilGetModuleDefinition_Contracts_Works()
        {
            try
            {
                ReflectionToCecil.GetModuleDefinition(null);
            }
            catch
            {                
            }

            Assert.IsTrue(this.failed);
        }

        private void ContractFailed(object sender, ContractFailedEventArgs e)
        {
            this.failed = true;
        }
#endif
    }
}
